const logins = require('../../../utils/logins')
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain

const page_init=(that)=>{
  wx.showLoading({title: '大量数据计算中……'})
  wx.request({
    url: domainName + 'questions/paper/retrieve/' + that.data.test_paper_id,
    data: {},
    method:'GET',
    header: {
      'AUTHORIZATION':'JWT ' + wx.getStorageSync('token'),
    },
    success: res => {
      wx.hideLoading()
      console.log('res',res.data)
      if(res.data.result == 1){
        that.setData({
          paper_info:res.data.data.test_paper,
          user_student:res.data.data.user_student,
        })
      }
      if(that.data.from!='from_jg'){
      let detail = res.data.detail
      wx.showModal({
        content: detail,
        showCancel: false,
        title: '提示',
        success:res=>{
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }
      })
      }

    },
  })
}

// 返回
const back=(that)=>{
  console.log('from', that.data.from)
  if(that.data.from == 'from_jg'){
    wx.navigateBack()
  }else{
    wx.reLaunch({
      url: '/pages/index/index',
      fail:err=>{
        console.log('err',err)
      }
    })
  }
}
  // 开始做题
const go_doTi=(that)=>{
  wx.redirectTo({
    url: '/pages/questions/paper_do/paper_do?test_paper_info=' + encodeURIComponent(JSON.stringify(that.data.paper_info)) + '&user_student=' + that.data.user_student
  })
}

// pages/testPaper_details/testPaper_details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + 'static/picture/app_icon/',
    from:'',
  },
  // 返回
  back(){getApp().get_vibration();back(this)},
  // 开始做题
  go_doTi(){getApp().get_vibration();go_doTi(this)},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      from:options.from, 
      windowWidth:wx.getWindowInfo().windowWidth,
    })

    this.data.test_paper_id = options.test_paper_id
    // 登录，并返回 this.data.userInfo
    logins.wechatLogin(this)

    // 定时检查userInfo，若存在，则加载页面初始化
    this.interval = setInterval(() => {
      if(this.data.userInfo){
        clearInterval(this.interval)
        page_init(this)
  }
    }, 500);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    console.log('用户点击右上角分享',e)
    getApp().get_vibration()
    return  {
      title: '试卷：' + this.data.paper_info.title,
      imageUrl:'/images/caac.png',
      path: '/pages/questions/paper_detail/paper_detail?test_paper_id=' + this.data.test_paper_id
    }
  }
})